<?php

require_once dirname(BASE_PATH) . '/custom/custom.php';

class shenzhenController extends Custom{
	


	function display($type,$condition){
		//
		if($type == 'goods'){
			$dirname = dirname(BASE_PATH) . '/custom/shenzhen/' . date('YmdHis') . '_goods';
			@mkdir($dirname);

			$content = file_get_contents(dirname(BASE_PATH) . '/custom/shenzhen/goods.tpl');
			
			$ret = array();

			foreach($condition as $c => $val){
				$con = array('goods_commonid' => $val);
				$data = array(
					'goods' => Model()->table('goods')->where($con)->find(),
					'goods_common' => Model()->table('goods_common')->where($con)->find(),
					'goods_extend' => Model()->table('custom_shenzhen_goods')->where($con)->find()

				);


				if(!$data['goods']){
					continue;
				}

				$data['store'] = Model('store')->where(array('store_id' => $data['goods']['store_id']))->find();

				$_this = $this;
				$cnt = preg_replace_callback("/\{\s*([^\}]+)\s*\}/",function($matched) use ($data,&$_this){

					//special
					if($matched[1] === 'image_list'){
						if(empty($data['goods_extend']['ImageList'])){
							return '';
						}
						$imgs = json_decode($data['goods_extend']['ImageList'],true);
						$xml = array();
						$xml[] = "<ImageList>";
						foreach($imgs as $key => $val){
							$xml[] = "<imageType>" . $val['imageType'] . "</imageType>";
							$xml[] = "<imageName>" . $val['imageName'] . "</imageName>";
							if(!empty($val['image'])){
								@$img_cnt = base64_encode(file_get_contents(dirname(BASE_PATH) . $val['image']));
							}
							else{
								$img_cnt = '';
							}
							$xml[] = "<image>" . $img_cnt . "</image>";
						}

						$xml[] = "</ImageList>";

						return implode($xml);
					}

					elseif($matched[1] === 'file_list'){
						if(empty($data['goods_extend']['AttachmentList'])){
							return '';
						}
						$imgs = json_decode($data['goods_extend']['AttachmentList'],true);
						$xml = array();
						$xml[] = "<AttachmentList>";
						foreach($imgs as $key => $val){
							$xml[] = "<attachmentType>" . $val['attachmentType'] . "</attachmentType>";
							$xml[] = "<attachmentName>" . $val['attachmentName'] . "</attachmentName>";
							if(!empty($val['attachment'])){
								@$img_cnt = base64_encode(file_get_contents(dirname(BASE_PATH) . $val['attachment']));
							}
							else{
								$img_cnt = '';
							}
							$xml[] = "<attachment>" . $img_cnt . "</attachment>";
						}

						$xml[] = "</AttachmentList>";

						return implode($xml);
					}

					if(strpos($matched[1],'.') !== false){
						$arr = explode('.',$matched[1]);
						switch($arr[0]){
							case 'config' :
								return $_this->config['config'][$arr[1]];

							case 'php' : 
								return call_user_func_array($arr[1], array($_this));

							case 'goods' : 
								if(isset($data['goods'][$arr[1]])){
									return $data['goods'][$arr[1]];
								} 
								elseif(isset($data['goods_common'][$arr[1]])){
									return $data['goods_common'][$arr[1]];
								}
								elseif(isset($data['goods_extend'][$arr[1]])){
									return $data['goods_extend'][$arr[1]];
								}
								break;
							case 'store' :
								if(isset($data['store'][$arr[1]])){
									return $data['store'][$arr[1]];
								}
						}
					}

					return $matched[1];
				},$content);

				$filename = 'CEB_CEB201_' . $this->config['config']['SenderID'] . '_' . $this->config['config']['ReceiverID'] . '_' . substr($GLOBALS['last_cebid'],15) . '.xml'; 
				file_put_contents($dirname . '/' . $filename, $cnt);

			}



			exit('生成成功，报文位于' . $dirname . '下');
		}
		elseif($type == 'order'){
			$dirname = dirname(BASE_PATH) . '/custom/shenzhen/' . date('YmdHis') . '_order';
			@mkdir($dirname);

			$content = file_get_contents(dirname(BASE_PATH) . '/custom/shenzhen/order.tpl');
			$ret = array();


			foreach($condition as $c => $val){
				$con = array('order_id' => $val);
				$data = array(
					'order' => Model()->table('order')->where($con)->find(),
					'order_extend' => Model()->table('order_common')->where($con)->find()

				);

				$data['store'] = Model('store')->where(array('store_id' => $data['order']['store_id']))->find();


				if(!$data['order']){
					continue;
				}



				$cnt = preg_replace_callback("/\{\s*([^\}]+)\s*\}/",function($matched) use ($val,$data,&$_this){
					if($matched[1] == 'goods_list'){
						$goods = Model()->table('order_goods')->where(array('order_id' => $val))->select();
						$xml = array('<OrderBillList>');
						$fields = array('gnum','itemNo','gno','gcode','gname','gmodel','barCode','brand','unit','currency','qty','price','total','giftFlag','note');
						$count = 1;
						foreach($goods as $g){
							$ginfo = Model('goods')->find($g['goods_id']);
							$goods_exten = Model('custom_shenzhen_goods')->where(array('goods_commonid' => $ginfo['goods_commonid']))->find();

							// $xml[] = 
							foreach($fields as $f){
								$xml[] = '<' . $f . '>';

								$ct = '';

								switch ($f) {
									case 'gnum':
										$ct = $count++;
										break;
									case 'itemNo':
										$ct = $ginfo['goods_commonid'];
										break;

									case 'barCode':
										$ct = $ginfo['sharp'];
										break;

									case 'qty':
										$ct = $g['goods_num'];
										break;

									case 'price':
										$ct = $g['goods_price'];
										break;

									case 'total':
										$ct = $g['goods_num'] * $g['goods_price'];
										break;

									default:
										if(isset($goods_exten[$f])){
											$ct = $goods_exten[$f];
										}
										# code...
										break;
								}
								$xml[] = $ct;
								$xml[] = '</' . $f . '>';
							}

						}
						$xml[] = '</OrderBillList>';
						return implode($xml);
					}
					if(strpos($matched[1],'.') !== false){

						$arr = explode('.',$matched[1]);
						switch($arr[0]){
							case 'config' :
								return $_this->config['config'][$arr[1]];

							case 'php' : 
								return call_user_func_array($arr[1], array($_this));

							case 'order' : 
								if(isset($data['order'][$arr[1]])){
									return $data['order'][$arr[1]];
								} 
								elseif(isset($data['order_extend'][$arr[1]])){
									return $data['order_extend'][$arr[1]];
								}
								else{
									$rinfo = unserialize($data['order_extend']['reciver_info']);
									$arr[1] = str_replace('re_','',$arr[1]);
									if(isset($rinfo[$arr[1]])){
										return $rinfo[$arr[1]];
									}
								}
								break;
							case 'store' :
								if(isset($data['store'][$arr[1]])){
									return $data['store'][$arr[1]];
								}
						}
					}
				},$content);



				$filename = 'CEB_CEB301_' . $this->config['config']['SenderID'] . '_' . $this->config['config']['ReceiverID'] . '_' . substr($GLOBALS['last_cebid'],15) . '.xml'; 
				file_put_contents($dirname . '/' . $filename, $cnt);

			}

			exit('生成成功，报文位于' . $dirname . '下');
			
		}
		// $config = Model('setting')->getListSetting();

	}


}


function send_time(){
	return app_time() . '000';
}


$last_cebid = '';

function cebid($t){
	$rand = '';
	for($i = 0; $i < 4; $i++){
		$rand .= rand(0,9);
	}
	return $GLOBALS['last_cebid'] = 'CEB201' . $t->config['config']['OrgCode'] . send_time() . $rand; 
}

function ceb_id($t){
	$rand = '';
	for($i = 0; $i < 4; $i++){
		$rand .= rand(0,9);
	}
	return $GLOBALS['last_cebid'] = 'CEB301' . $t->config['config']['OrgCode'] . send_time() . $rand; 
}

function app_time(){
	return date('YmdHis');
}